package com.test.queue;

public class CircleQueue {
    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;

    public CircleQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }

    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    public boolean isEmpty() {
        return rear == front;
    }

    public int size() {
        return (rear - front) % maxSize;
    }

    public void addQueue(int i) {
        if (isFull()) {
            System.out.println("队列已满~~");
            return;
        }
        arr[rear] = i;
        rear = (rear + 1) % maxSize;
    }

    public int getQueue() {
        if (isEmpty())
            throw new RuntimeException("队列空的，没有数据~~");
        int val = arr[front];
        front = (front + 1) % maxSize;
        return val;
    }

    public void list() {
        for (int i = front; i < front + size(); i++) {
            System.out.println(arr[i % maxSize]);
        }
    }

    public static void main(String[] args) {
        CircleQueue circleQueue = new CircleQueue(10);
        circleQueue.addQueue(1);
        circleQueue.addQueue(2);
        circleQueue.addQueue(3);
        circleQueue.addQueue(4);
        circleQueue.addQueue(5);
        circleQueue.addQueue(6);
        circleQueue.addQueue(7);
        circleQueue.list();
    }
}
